package cn.jll.dev.utils.office;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @description: excel 操作工具
 * @author: jinleilei
 * @create: 2019-05-29 15:18:28
 * @Version: v1.0
 **/
public class ExcelUtils {
    /**
     * Excel 2003
     */
    private static final String EXCEL_XLS = "xls";

    /**
     * Excel 2007/2010
     */
    private static final String EXCEL_XLSX = "xlsx";

    public static Workbook getWorkbok(File file) throws IOException {
        Workbook wb = null;
        if (file.getName().endsWith(EXCEL_XLS)) {
            if (file.exists()) {
                FileInputStream in = new FileInputStream(file);
                wb = new HSSFWorkbook(in);
            } else {
                wb = new HSSFWorkbook();
            }
        } else if (file.getName().endsWith(EXCEL_XLSX)) {
            if (file.exists()) {
                FileInputStream in = new FileInputStream(file);
                wb = new XSSFWorkbook(in);
            } else {
                wb = new XSSFWorkbook();
            }
        }
        return wb;
    }

}
